import java.util.PriorityQueue;
import java.util.Scanner;

/**
 * @author vivi
 * @description
 * @date 2024/5/25
 */
public class demo2 {
    public static void main (String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        PriorityQueue<Long> heap = new PriorityQueue<>();
        while(n-- != 0)
        {
            long x = in.nextLong();
            heap.offer(x);
        }
        // 构建最优⼆叉树 / 构建哈夫曼树
        long ret = 0;
        while(heap.size() > 1)
        {
            long t1 = heap.poll();
            long t2 = heap.poll();
            heap.offer(t1 + t2);
            ret += t1 + t2;
        }
        System.out.println(ret);

    }
}
